import __builtin__
from client.utils.assethandling import createBullet
from direct.showbase.DirectObject import DirectObject
import client.unitsystem.unit
from panda3d.core import Vec3, VBase3
import abc


class Projectile(object):

    def __init__(self, name, node, speed, target, manager):
        self.name = name
        self.projectileNode = node
        self.speed = speed
        if isinstance(target, client.unitsystem.unit.Unit):
            self.target = target.unit_object
        else:
            self.target = target.towerNode
        self.manager = manager
        self.tarpos = Vec3(self.target.getPos().x, self.target.getPos().y, 0)

    def destroy(self):
        self.projectileNode.remove_node()

    def set_pos(self, pos):
        self.projectileNode.setPos(__builtin__.render, pos)

    def update(self, dt):
        #self.projectileNode.setPos(self.projectileNode, Vec3(1,0,0)*dt)
        print "UPDATEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEe"
        if self.target is not None and not self.target.is_empty():
            self.tarpos = Vec3(self.target.getPos().x, self.target.getPos().y, 0)
        if not self._check_reached():
            dir = self.tarpos - self.projectileNode.getPos()
            VBase3.normalize(dir)
            self.projectileNode.setPos(self.projectileNode, dir * (self.speed * dt))

    def _check_reached(self):
        dis = self.tarpos - self.projectileNode.getPos()
        leng = VBase3.length(dis)
        if leng < 0.2:
            if self.target is not None:
                self.manager.destroy_projectile(self)
            return True
        return False
